﻿// Write a program, that reads from the console an array of N integers and an integer K, 
// sorts the array and using the method Array.BinSearch() finds the largest number in the array which is ≤ K. 

using System;

class FindLargestNumberInArray
{
    static void Main()
    {
        Console.Write("Enter Array size (N): ");
        int size = int.Parse(Console.ReadLine());
        int[] array = new int[size];
        for (int i = 0; i < array.Length; i++)
        {
            Console.Write("Array[{0}] = ", i);
            array[i] = int.Parse(Console.ReadLine());
        }

        Console.Write("Enter largest number limit (K): ");
        int largestNumberLimit = int.Parse(Console.ReadLine());

        Array.Sort(array);
        int index = Array.BinarySearch(array, largestNumberLimit);

        if (index < -1)
        {
            Console.WriteLine("Largest number equal with or lower than {0} is {1}", largestNumberLimit, array[~index - 1]);
        }
        else if (index == -1)
        {
            Console.WriteLine("Not exist number meets the criteria");
        }
        else
        {
            Console.WriteLine("Largest number equal with or lower than {0} is {1}", largestNumberLimit, array[index]);
        }
    }
}